import json
import sys

def parse_and_format_json(json_data):
    # 从外层字典中提取 "Data" 字符串
    data_str = json_data["body"]["Data"]
    # 解析内层 JSON 字符串为字典
    inner_data = json.loads(data_str)

    # 提取并格式化需要的部分
    formatted_data = {
        "algo_version": inner_data["algo_version"],
        "pages": []
    }

    # 遍历内层数据的 page_list
    for page in inner_data["page_list"]:
        formatted_page = {
            "angle": page["angle"],
            "doc_index": page["doc_index"],
            "height": page["height"],
            "orgHeight": page["orgHeight"],
            "orgWidth": page["orgWidth"],
            "page_id": page["page_id"],
            "subjects": []
        }

        for subject in page["subject_list"]:
            formatted_subject = {
                "content_list_info": subject["content_list_info"],
                "ids": subject["ids"],
                "is_multipage": subject["is_multipage"],
                "prism_wordsInfo": subject["prism_wordsInfo"],
                "text": subject["text"]
            }
            formatted_page["subjects"].append(formatted_subject)

        formatted_data["pages"].append(formatted_page)

    # 返回格式化的 JSON 字符串
    return json.dumps(formatted_data, indent=4, ensure_ascii=False)

file_path = sys.argv[1]
# 文件路径
in_file_path = file_path+"_fixed_tmp.json"
out_file_path = file_path+"_normalize_json.json"

# 读取外层 JSON 数据
with open(in_file_path, 'r', encoding='utf-8') as f:
    json_data = json.load(f)

# 调用函数并写入结果
formatted_json = parse_and_format_json(json_data)
with open(out_file_path, "w", encoding='utf-8') as f:
    f.write(formatted_json)
print(formatted_json)
